package com.haowu.customer.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.haowu.common.core.domain.R;
import com.haowu.common.core.validate.AddGroup;
import com.haowu.common.core.validate.EditGroup;
import com.haowu.common.core.web.controller.BaseController;
import com.haowu.common.log.annotation.Log;
import com.haowu.common.log.enums.BusinessType;
import com.haowu.common.mybatis.core.page.PageQuery;
import com.haowu.common.mybatis.core.page.TableDataInfo;
import com.haowu.customer.domain.vo.HwAppCustomerVo;
import com.haowu.customer.domain.bo.HwAppCustomerBo;
import com.haowu.customer.service.IHwAppCustomerService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 用户会员控制器
 * 前端访问路由地址为:/customer/hwAppCustomer
 *
 * @author lky
 * @date 2023-02-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hwAppCustomer")
public class HwAppCustomerController extends BaseController {

    private final IHwAppCustomerService iHwAppCustomerService;

    /**
     * 查询用户会员列表
     */
    @SaCheckPermission("customer:appCustomer:list")
    @GetMapping("/list")
    public TableDataInfo<HwAppCustomerVo> list(HwAppCustomerBo bo, PageQuery pageQuery) {
        return iHwAppCustomerService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取用户会员详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("customer:appCustomer:query")
    @GetMapping("/{id}")
    public R<HwAppCustomerVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iHwAppCustomerService.queryById(id));
    }

    /**
     * 新增用户会员
     */
    @SaCheckPermission("customer:appCustomer:add")
    @Log(title = "用户会员", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HwAppCustomerBo bo) {
        return toAjax(iHwAppCustomerService.insertByBo(bo));
    }

    /**
     * 修改用户会员
     */
    @SaCheckPermission("customer:appCustomer:edit")
    @Log(title = "用户会员", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HwAppCustomerBo bo) {
        return toAjax(iHwAppCustomerService.updateByBo(bo));
    }

    /**
     * 删除用户会员
     *
     * @param ids 主键串
     */
    @SaCheckPermission("customer:appCustomer:remove")
    @Log(title = "用户会员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iHwAppCustomerService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
